<template>
	<view class="main" style="background-color: #f8f8f8;min-height: 100vh; display:block;">
		<view class="user_header_box">
				<!-- 登录 -->
				<block v-if="isLogin">
					<view class="avatar_box" @click="goEditor">
						<image class="img" :src="headimg" mode=""></image>
					</view>
					<view class="user_info">
						<view class="name flex flex-row justify-between" @click="goEditor">
							<view>{{username}}</view>
							<view class="relative left-2 top-1"><u-icon name="edit-pen-fill" color="#FFE2BF" size="22"></u-icon></view>
						</view>
						<view class="uid" @click="goEditor">
							{{hiddenPhone(userphone)}}
						</view>
						<view class="uid">
							{{vipname}} / {{userData.jifen}}积分
						</view>
					</view>
				</block>
				<!-- 未登录 -->
				<block v-else>
						<view class="avatar_box">
							<image class="img" src="../../static/user/tab_icon/account.png" mode=""></image>
						</view>
						<view class="user_info">
							<view class="name mb-1">
								游客
							</view>
							<view class="uid">
								<button open-type="getPhoneNumber" scope="phoneNumber" @getphonenumber="getPhoneEvent" class="wx">
								   点击登录/注册
								</button>
							</view>
						</view>
				</block>
		</view>
		<view class="user_amount_box">
			<view class="user_level flex flex-row justify-between">
				<view><image class="icon" src="../../static/user/level_icon.png"></image></view>
				<view class="level_desc_box justify-start w-full"  @click="setJiFenZero">
					<view class="title basis-1/2">
						点击这里积分归0
					</view>
					<view class="desc basis-1/2">
						十八项特权
					</view>
				</view>
				<view class="basis-1/4">
					<u-button type="primary" text="添加积分" :customStyle="btnCustomStyle" @click="addjifen"></u-button>
				</view>
				<!-- <image class="right_icon" src="../../static/user/right.png" mode=""></image> -->
			</view>
			<view class="amount_box">
				<!-- <view class="amount">
					360,000.00 <text style="font-size: 26rpx;">元</text>
				</view>
				<view class="now_with">
					立即提现
				</view> -->
				<view class="status" @click="GoOrder(0)">
					<view class="number">{{allOrders}}</view>
					<view class="textword">全部</view>
				</view>
				<view class="status" @click="GoOrder(1)">
					<view class="number">{{waitPayOrders}}</view>
					<view class="textword">待付款</view>
				</view>
				<view class="status" @click="GoOrder(2)">
					<view class="number">{{waitSendOrders}}</view>
					<view class="textword">待发货</view>
				</view>
				<view class="status" @click="GoOrder(3)">
					<view class="number">{{deliveredOrders}}</view>
					<view class="textword">已发货</view>
				</view>
			</view>
		</view>

		<view class="user_tab_box">
			<view class="tab_title">
				常用服务
			</view>
			<view class="tab_list_box">
				<!-- <view class="list_item">
					<image class="icon" src="../../static/user/tab_icon/amount.png" mode=""></image>
					<view class="text">我的钱包</view>
				</view>
				<view class="list_item">
					<image class="icon" src="../../static/user/tab_icon/xieyi.png" mode=""></image>
					<view class="text">注册协议</view>
				</view> -->
				<view class="list_item" @click="GoUrl('../shop_allorder/shop_allorder')">
					<image class="icon" src="../../static/icons/sell.png" mode=""></image>
					<view class="text">我的订单</view>
				</view>
				<view class="list_item" @click="GoUrl('../shop_myfav/shop_myfav')">
					<image class="icon" src="../../static/icons/fav.png" mode=""></image>
					<view class="text">我的收藏</view>
				</view>
				<view class="list_item">
					<image class="icon" src="../../static/icons/kf.png" mode=""></image>
					<view class="text">客服咨询</view>
				</view>
				<view class="list_item" @click="GoUrl('../shop_selectedAddress_new/shop_selectedAddress_new')">
					<image class="icon" src="../../static/icons/adr.png" mode=""></image>
					<view class="text">收货地址</view>
				</view>
				<view class="list_item" @click="GoUrl('../shop_coupon/shop_coupon')">
					<image class="icon" src="../../static/icons/yhj.png" mode=""></image>
					<view class="text">优惠卷</view>
				</view>
				<view class="list_item" @click="GoUrl('../shop_dotask/shop_dotask')">
					<image class="icon" src="../../static/icons/task.png" mode=""></image>
					<view class="text">任务</view>
				</view>
				<view class="list_item" @click="GoUrl('../shop_moneybag/shop_moneybag')">
					<image class="icon" src="../../static/icons/qb.png" mode=""></image>
					<view class="text">钱包</view>
				</view>
				<view class="list_item" @click="GoUrl('../shop_setlist/shop_setlist')">
					<image class="icon" src="../../static/icons/set.png" mode=""></image>
					<view class="text">设置</view>
				</view>
			</view>
		</view>
		
		<view class="w-[90%] m-auto flex flex-wrap justify-between items-center paraBtn">
			<view><u-button type="primary" text="参数设置" @click="GoUrl('/pages/shop_set/shop_set',2)"></u-button></view>
			<view><u-button type="primary" text="绑定扫码" @click="GoUrl('/pages/shop_scanQrcode/shop_scanQrcode')"></u-button></view>
			<view><u-button type="primary" text="读取二维码到本机" @click="GoUrl('/pages/shop_shop_scanData2SaveLocal/shop_shop_scanData2SaveLocal')"></u-button></view>
			<view><u-button type="primary" text="返佣结果" @click="GoUrl('/pages/shop_result/shop_result')"></u-button></view>
		</view>
		
		<!-- <view class="loan_box"> 
			<view class="loan_title_box">
				<view class="title">
					热门产品
				</view>
				<view class="link" @click="GoUrl('../class/class',2)">
					查看更多
				</view>
			</view>
			<view class="loan_list">
				<view class="list_item" v-for="(item,index) in productList.data" :key="index">
					<view class="item_head">
						<image class="pic" :src="item.productimg" mode=""></image>
						<view class="loan_info">
							<view class="name">
								{{item.productname}}
							</view>
							<view class="text-sm date">
								{{limtStrLen(item.productdesc,50)}}
							</view>
						</view>
						<view class="join_btn">
							购买
						</view>
					</view>
					<view class="item_bottom">
						<view class="bottom_item">
							<view class="names">
								价格
							</view>
							<view class="amount">
								{{item.productprice}}
							</view>
						</view>
						<view class="bottom_item">
							<view class="names">
								服务周期
							</view>
							<view class="amount">
								3-24
							</view>
						</view>
						<view class="bottom_item">
							<view class="names">
								售后服务
							</view>
							<view class="amount">
								1年
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		-->
	</view>
</template>

<script setup lang='ts'>
import { ref,reactive } from 'vue'
import { onLoad,onShow } from '@dcloudio/uni-app'
import { GoUrl,Toast,hiddenPhone,storeSet,storeGet,limtStrLen } from '@/utils/utils'
import { getPhoneNumber,getAllProducts,getAllOrdersData } from '@/utils/apiFunction'
import { userStore } from '@/stores/userStore'
let { hasLogin,userData } = userStore();
const store = userStore()

//用户订单
let allOrders=ref<number>(0);
let waitPayOrders=ref<number>(0);
let waitSendOrders=ref<number>(0);
let deliveredOrders=ref<number>(0);


let productList=reactive({data:[
	{
		productimg:"https://img1.baidu.com/it/u=4026044884,3253279615&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=531",
		productname:"8核CPU电脑",
		productdesc:"Apple MacBook Air 13.6 8核M2芯片(8核图形处理器) 8G 256G SSD 银色 轻薄学习办公笔记本电脑 MLXY3CH/A",
		productprice:1988
	},
	{
		productimg:"https://img10.360buyimg.com/n1/jfs/t1/124498/26/29914/42912/64d1ea5eFa97d8d80/467da1a491c43d29.jpg.avif",
		productname:"8核CPU电脑",
		productdesc:"Apple MacBook Air 13.6 8核M2芯片(8核图形处理器) 8G 256G SSD 银色 轻薄学习办公笔记本电脑 MLXY3CH/A",
		productprice:2188
	},
	{
		productimg:"https://img10.360buyimg.com/n1/jfs/t1/155692/27/37089/204616/64658e1fFca600f30/6459bc17f1cedae9.jpg.avif",
		productname:"8核CPU电脑",
		productdesc:"Apple MacBook Air 13.6 8核M2芯片(8核图形处理器) 8G 256G SSD 银色 轻薄学习办公笔记本电脑 MLXY3CH/A",
		productprice:3088
	}
]})
let userinfo=reactive({}) //data:{'name':'Daniel'}
let isLogin=ref(true);
let headimg=ref("../../static/user/avatar.png");
let username=ref("花落红尘");
let userphone=ref("13800138000");
let vipname=ref("普通用户");
//console.log("userinfo",userinfo.hasOwnProperty('data'))
let loginCode=ref("");
let btnCustomStyle=reactive<object>({
	"border":"0px",
	"background":"#FFE2BF",
	"color":"#000"
});

const setJiFenZero=()=>{
	userData.jifen=0;
	uni.setStorageSync("userData",userData);
}

const addjifen=()=>{
	store.addjifen();
	//userData.jifen=store.getjifen(); //实时更新缓存
	uni.setStorageSync("userData",userData);
}


onLoad(()=>{
	/* uni.login({
		  provider: 'weixin',
		  onlyAuthorize:true,
		  success:e=> {
			//登录。注册会员信息
			if(e.errMsg==='login:ok' && e.code!=null){
				loginCode.value=e.code;
				console.log("微信 login code:",loginCode.value);							
			}
		  }
	}); */
	
	//读取用户所有订单信息
	/* if(store.userData.userinfo.id!=null){
		getUserOrders();
	} */
	
	
	//获取产品信息
	/*  getAllProducts().then(res=>{
		//从一定时间内缓存中读取商品，不用重复读取数据库
		let storeData:any=storeGet("mineProducts");
		if(storeData!=null && storeData!=''){
			productList.data=storeData;
			console.log("从缓存读取数据",productList.data)
			return;
		}
		console.log("从数据库读取产品信息",res)
		if(res.data.code===200){
			//给商品列表赋值
			productList.data = res.data.data;
			//缓存商品信息
			storeSet("mineProducts",productList.data,300*1000); //1000 * 60 * 5 =300*1000秒=300秒=5分钟
	    }
	});	  */
	
})

onShow(()=>{
	
	console.log("userData",userData);
	uni.setStorageSync("userData",userData);
	
	let orders=uni.getStorageSync("orderProductList") || null;
	if(orders!=null){
		allOrders.value=orders.length;
	}
	
	/* userinfo=store.userData;
	//比较时间是否过期，过期需要重新登录
	let expireTime:number=store.userData.expireTime as number //获取最大过期时间
	console.log('expireTime: ',expireTime);
	let nowTime:number=new Date().getTime()
	console.log('nowTime: ',nowTime);
	let saveTime:number=store.userData.saveTime as number
	console.log('saveTime: ',saveTime);
	console.log("(nowTime-saveTime)",(nowTime-saveTime),expireTime);
	let isExpire:boolean=(nowTime-saveTime)>expireTime?true:false;
	console.log('是否过期isExpire: ',isExpire);
	if(!isExpire){
		if(JSON.stringify(userinfo) != "{}"){
			if(userinfo.isLogin!=null && userinfo.isLogin===true){
				isLogin.value=true
				updateUserInfo(userinfo.userinfo);
			}
		}
	}else{
		store.remove(); //清空缓存信息
		userinfo=store.userData;
		isLogin.value=false;
	} */
/* 	if(userinfo.hasOwnProperty('data')){
		isLogin.value=true;
	} */
})


//跳转订单
const GoOrder=(num:number)=>{
	switch(num){
		case 0:
			GoUrl("../shop_allorder/shop_allorder?statenum="+num);
		case 1:
			GoUrl("../shop_allorder/shop_allorder?statenum="+num);
		case 2:
			GoUrl("../shop_allorder/shop_allorder?statenum="+num);
		case 3:
			GoUrl("../shop_allorder/shop_allorder?statenum="+num);
		default:
	}
}


//读取所有订单信息
const getUserOrders=()=>{
	let paraData:any={
		userid:store.userData.userinfo.id
	}
	getAllOrdersData(paraData).then(res=>{
		console.log("读取用户所有订单",res);
		let orderDatas=res.data.AlllistOrders;
		allOrders.value=orderDatas.length;
		waitPayOrders.value=orderDatas.filter(item=>item.paystatue==='未支付').length;
		waitSendOrders.value=orderDatas.filter(item=>item.paystatue==='已支付' && item.deliverstatue==='未发货').length;
		deliveredOrders.value=orderDatas.filter(item=>item.paystatue==='已支付' && item.deliverstatue==='已发货').length;
	});
}


const updateUserInfo=(userData:any)=>{
		headimg.value=userData.headimg
		username.value=userData.urealname
		userphone.value=userData.uphone	
}

const getPhoneEvent=(e:any)=>{
	getPhoneNumber(e,loginCode,(res)=>{
		isLogin.value=true;
		Toast("登录成功");
		//永久化保存数据
		store.userData.userinfo=res.data.data.userinfo;
		store.userData.expireTime=res.data.data.expireTime;
		store.userData.isLogin=true; //追加字段
		store.userData.saveTime=new Date().getTime();
		
		//获取用户订单信息
		getUserOrders();
		
		//更新用户信息
		updateUserInfo(res.data.data.userinfo);
		
		//如果用户是从浏览产品，响应登录而来，则回到当前产品页面
		if(store.backRouter!=null && store.backRouter!=''){
			let TempUrl=store.backRouter+"";
			TempUrl+=TempUrl.indexOf("?")!=-1?"&noRecord=yes":"?noRecord=yes";
			console.log("重置前Url",TempUrl);
			store.backRouter=null; //重置状态
			let Jnumer:number=1;
			if(TempUrl.indexOf("index")!=-1 || TempUrl.indexOf("class")!=-1 || TempUrl.indexOf("cart")!=-1 || TempUrl.indexOf("mine")!=-1){
				Jnumer=2;
			}
			GoUrl(TempUrl,Jnumer);
		}
	});
}

const goEditor=function(){
	uni.$tools.GoUrl("/pages/shop_edituserinfo/shop_edituserinfo");
}





</script>

<style lang='scss' scoped>
	@import "shop_mine.scss";
</style>